package com.ctshk.common.utils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class APIUtil {
    public static String callAPIFunc(String contentType,String pathUrl,String param,String methodType){
        String result = "";
        {
            URL url =null;
            try
            {
                //创建连接
                url = new URL(pathUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.setUseCaches(false);
                conn.setRequestMethod(methodType);
                conn.setRequestProperty("Content-Type", contentType);
                conn.connect();
                //参数
                DataOutputStream out =new DataOutputStream(conn.getOutputStream());
                out.writeBytes(param);
                out.flush();
                out.close();
                // 获取响应
                BufferedReader reader =new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String lines;
                StringBuffer sb =new StringBuffer();
                //读取结果
                while
                ((lines = reader.readLine()) !=null){
                    lines =new String(lines.getBytes(),"utf-8");
                    sb.append(lines);
                }
                result = sb.toString();
                // 关闭连接
                reader.close();
                conn.disconnect();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
//    Westminster测试：
    public static void main(String[] args) {
    String param = "<OTA_HotelDescriptiveInfoRQ xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" Version=\"0\"\n" +
                "xmlns=\"http://www.opentravel.org/OTA/2003/05\" primaryLangID=\"en-us\">\n" +
                "<POS>\n" +
                " <Source>\n" +
                "\t<RequestorID ID=\"api@ctshk.com\" MessagePassword=\"111111\"/>\n" +
                " </Source>\n" +
                "</POS>\n" +
                "<HotelDescriptiveInfos>\n" +
                "<HotelDescriptiveInfo HotelCode=\"00001\"/>\n" +
                "</HotelDescriptiveInfos>\n" +
                "</OTA_HotelDescriptiveInfoRQ>";
    String getRoom = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
            "<OTA_HotelAvailRQ xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
            "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" Version=\"0\"\n" +
            "xmlns=\"http://www.opentravel.org/OTA/2003/05\">\n" +
            "<POS>\n" +
            " <Source ISOCountry=\"\">\n" +
            " <RequestorID ID=\"api@ctshk.com\" MessagePassword=\"111111\"/>\n" +
            " </Source>\n" +
            "</POS>\n" +
            "<AvailRequestSegments>\n" +
            " <AvailRequestSegment>\n" +
            " <HotelSearchCriteria>\n" +
            " <Criterion>\n" +
            " <HotelRef HotelCityCode=\"HKG\" HotelCode=\"00005\" HotelName=\"\" />\n" +
            " <StayDateRange Start=\"2021-04-01\" End=\"2021-04-02\" />\n" +
            " <RatePlanCandidates>\n" +
            " <RatePlanCandidate RatePlanID=\"\" />\n" +
            " </RatePlanCandidates>\n" +
            " <RoomStayCandidates>\n" +
            " <RoomStayCandidate>\n" +
            " <GuestCounts>\n" +
            " <GuestCount AgeQualifyingCode=\"10\" Count=\"2\" />\n" +
            " </GuestCounts>\n" +
            " </RoomStayCandidate>\n" +
            " </RoomStayCandidates>\n" +
            " </Criterion>\n" +
            " </HotelSearchCriteria>\n" +
            " </AvailRequestSegment>\n" +
            "</AvailRequestSegments>\n" +
            "</OTA_HotelAvailRQ>\n";
        String s = callAPIFunc("text/xml;charset=utf-8", "http://ws.adholidays.com/hotelapidev/otaservice.aspx", getRoom,"POST");
        System.out.println(s);
//    长隆测试：（返回数据需要解密）
//    String CONTENT_WITH_NO_SIGN ="{\"mer_no\":\"9000000242\",\"method\":\"chimelong.hotel.product.info.get\",\"sign\":\"\",\"nonce_str\":\"80c5a87d22ed461bbe91ec691a4d9874\",\"timestamp\":\"2021-02-04 10:13:01\",\"ver_no\":\"1.0\",\"body\":\"{\\\"productCodes\\\":[\\\"G202TSTAE00198_1000000425\\\",\\\"G302FSSAF00132_1000000376\\\"]}}\"}";
//        String CONTENT_WITH_NO_SIGN2 ="{\"mer_no\":\"9000000002\",\"method\":\"chimelong.hotel.product.info.get\",\"sign\":\"\",\"nonce_str\":\"80c5a87d22ed461bbe91ec691a4d9874\",\"timestamp\":\"2021-03-26 10:13:01\",\"ver_no\":\"1.0\",\"body\":\"{\\\"productCodes\\\":[\\\"G302TDTAF00132_1000000376\\\"]}}\"}";
//    String CONTENT_WITH_NO_SIGN3 ="{\"mer_no\":\"9000000242\",\"method\":\"chimelong.hotel.hotel.info.get\",\"sign\":\"\",\"nonce_str\":\"80c5a87d22ed461bbe91ec691a4d9874\",\"timestamp\":\"2021-03-12 10:13:01\",\"ver_no\":\"1.0\",\"body\":\"{}\"}";
//    Integer statausInt = 0;
//    String CONTENT_WITH_NO_SIGN4 ="{\"mer_no\":\"9000000242\",\"method\":\"chimelong.hotel.product.change.notice\",\"sign\":\"\",\"nonce_str\":\"80c5a87d22ed461bbe91ec691a4d9874\",\"timestamp\":\"2021-03-12 10:13:01\",\"ver_no\":\"1.0\",\"body\":\"{\\\"notice\\\":[\\\"{\\\"status\\\":\\\"1\\\"},{\\\"productDate\\\":\\\"2021-03-12 10:13:01\\\"},{\\\"productCategory\\\":\\\"T\\\"}]}\"}";
//        String CONTENT_WITH_NO_SIGN ="{\"mer_no\":\"9000000242\",\"method\":\"chimelong.hotel.order.verify\",\"sign\":\"\",\"nonce_str\":\"80c5a87d22ed461bbe91ec691a4d9874\",\"timestamp\":\"2021-03-12 10:13:01\",\"ver_no\":\"1.0\",\"body\":\"{{ \\\\\\\"remark\\\\\\\": \\\\\\\"aaaaaa\\\\\\\", \\\\\\\"name\\\\\\\": \\\\\\\"xx\\\\\\\", \\\\\\\"mobile\\\\\\\": \\\\\\\"13666641818\\\\\\\", \\\\\\\"email\\\\\\\": \\\\\\\"xxx@email.com\\\\\\\", \\\\\\\"productData\\\\\\\": [{ \\\\\\\"entryNumber\\\\\\\": \\\\\\\"1\\\\\\\", \\\\\\\"productCode\\\\\\\": \\\\\\\"ZHH0020921\\\\\\\", \\\\\\\"begDate\\\\\\\": \\\\\\\"2018-12-18\\\\\\\", \\\\\\\"endDate\\\\\\\": \\\\\\\"2018-12-19\\\\\\\", \\\\\\\"settlePrice\\\\\\\": 29.8, \\\\\\\"seasonCode\\\\\\\": \\\\\\\"2GZ3030\\\\\\\", \\\\\\\"quantity\\\\\\\": 1, \\\\\\\"totalMoney\\\\\\\": 29.8, \\\\\\\"addBedNum\\\\\\\": 0, \\\\\\\"adults\\\\\\\": 1, \\\\\\\"children\\\\\\\": 0, \\\\\\\"dayPriceData\\\\\\\": [{ \\\\\\\"productCode\\\\\\\": \\\\\\\"XiAO\\\\\\\", \\\\\\\"roomDate\\\\\\\": \\\\\\\"075593864321\\\\\\\", \\\\\\\"settlePrice\\\\\\\": 1, \\\\\\\"currency\\\\\\\": CNY\\\\\\\"430304199607251988\\\\\\\", \\\\\\\"otaPrice\\\\\\\": null , \\\\\\\"breakFastNum\\\\\\\": null, \\\\\\\"stockLevel\\\\\\\": null, \\\\\\\"stock\\\\\\\": null}], \\\\\\\"personInfoData\\\\\\\": [{ \\\\\\\"name\\\\\\\": \\\\\\\"XiAO\\\\\\\", \\\\\\\"mobile\\\\\\\": \\\\\\\"075593864321\\\\\\\", \\\\\\\"certCardType\\\\\\\": 1, \\\\\\\"certCardNo\\\\\\\": \\\\\\\"430304199607251988\\\\\\\", \\\\\\\"email\\\\\\\": null }] }] }\"}";


//    String SELECT_PRODUCT_PRICE_BY_DAY ="{\"mer_no\":\"9000000242\",\"method\":\"chimelong.hotel.product.info.get\",\"sign\":\"\",\"nonce_str\":\"80c5a87d22ed461bbe91ec691a4d9874\",\"timestamp\":\"2021-03-12 10:13:01\",\"ver_no\":\"1.0\",\"body\":\"{\\\"productCodes\\\":[\\\"G202TSTAE00198_1000000425\\\"]}}\"}";
//        String CREATEORDER ="{\"mer_no\":\"9000000242\",\"method\":\"chimelong.hotel.order.create\",\"sign\":\"\",\"nonce_str\":\"80c5a87d22ed461bbe91ec691a4d9874\",\"timestamp\":\"2021-03-12 10:13:01\",\"ver_no\":\"1.0\",\"body\":\"{" +
//                "\\\"otaOrderId\\\": \\\"gzl123456\\\"," +
//                "\\\"name\\\": \\\"zhangsan\\\"," +
//                "\\\"mobile\\\": \\\"13189781311\\\"," +
//                "\\\"orderMoney\\\": \\\"979\\\"," +
//                "\\\"currency\\\": \\\"HKD\\\"," +
//                "\\\"productData\\\": [{" +
//                "\\\"productCode\\\": \\\"GGZH001TDH0002_1000000423\\\"," +
//                "\\\"begDate\\\": \\\"2021-04-01\\\"," +
//                "\\\"endDate\\\": \\\"2021-04-02\\\"," +
//                "\\\"settlePrice\\\": 979," +
//                "\\\"otaPrice\\\": 979," +
//                "\\\"quantity\\\": 1," +
//                "\\\"totalMoney\\\": 979," +
//                "\\\"personInfoData\\\": [{" +
//                "\\\"name\\\": \\\"zhangsan\\\"," +
//                "\\\"mobile\\\": \\\"13189781311\\\"" +
//                "}]" +
//                "}]" +
//                "}\"}";
//
//    String cancelOrder ="{\"mer_no\":\"9000000242\",\"method\":\"chimelong.hotel.order.cancel\",\"sign\":\"\",\"nonce_str\":\"80c5a87d22ed461bbe91ec691a4d9874\",\"timestamp\":\"2021-03-26 10:13:01\",\"ver_no\":\"1.0\",\"body\":\"{\\\"chimeOrderId\\\":\\\"GDH2021032600000009\\\"}\"}";
//    String updateOrder ="{\"mer_no\":\"9000000242\",\"method\":\"chimelong.hotel.product.price.get\",\"sign\":\"\",\"nonce_str\":\"80c5a87d22ed461bbe91ec691a4d9874\",\"timestamp\":\"2021-03-26 10:13:01\",\"ver_no\":\"1.0\",\"body\":\"{" +
//            "\\\"chimeOrderId\\\":\\\"GDH2021032600000010\\\"" +
//            "\\\"currency\\\":\\\"HKD\\\"" +
//            "\\\"productData\\\": [{" +
//            "\\\"targetProductCode\\\":\\\"GGZH001TDH0002_1000000423\\\"," +
//            "\\\"targetBegDate\\\":\\\"2021-03-31\\\"," +
//            "\\\"targetEndDate\\\":\\\"2021-03-31\\\"," +
//            "\\\"targetSettlePrice\\\":\\\"1113\\\"," +
//            "\\\"targetQuantity\\\":\\\"1\\\"," +
//            "\\\"targetPersonInfoData\\\":[{" +
//
//
//            "}]" +
//            "}]" +
//            "}\"}";
//    String GetPriceByDay ="{\"mer_no\":\"9000000242\",\"method\":\"chimelong.hotel.product.price.get\",\"sign\":\"\",\"nonce_str\":\"80c5a87d22ed461bbe91ec691a4d9874\",\"timestamp\":\"2021-03-26 10:13:01\",\"ver_no\":\"1.0\",\"body\":\"{\\\"getData\\\":[{\\\"productCode\\\":\\\"GZHH002FSS0029_1000000428\\\",\\\"begDate\\\":\\\"2021-03-30\\\",\\\"endDate\\\":\\\"2021-03-31\\\"}]}\"}";
//    System.out.println(GetPriceByDay);
//    String signAppendStr = new APISignUtil().contentJointTest(CREATEORDER);
//    String toData = new APIAesUtil().aesEncodeBase64(signAppendStr, "1d70b9997df4946824810ebb069dbe28");//SIGN_KEY:"1d70b9997df4946824810ebb069dbe28"（长隆提供）
//    String params =
//            "{" +
//                    "\"mer_no\":\"9000000242\","+
//                    "\"data\":\""+toData+"\""+
//                    "}";
//    String contentType = "application/json;charset=utf-8";
//    String path = "http://47.244.225.190:8080/hub/product/ota/call2/gsa/chimelong/api.do";
//    String returnStr = new APIAesUtil().aesDecodeBase64(callAPIFunc("application/json;charset=utf-8",path,params,"POST"), "1d70b9997df4946824810ebb069dbe28");
//    System.out.println("返回内容解密："+returnStr);
      //龙腾测试
//        String params = "xmlRequest=<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
//                "<CNRequest>\n" +
//                "  <!--用户信息-->\n" +
//                "  <ActionName>RatePlanSearch</ActionName>\n" +
//                "  <IdentityInfo>\n" +
//                "    <AppId>1</AppId>\n" +
//                "    <SecurityKey>369b469c-51b2-43cd-9677-934ca17f2651</SecurityKey>\n" +
//                "    <UserName>EN000001</UserName>\n" +
//                "    <PassWord>e10adc3949ba59abbe56e057f20f883e</PassWord>\n" +
//                "    <Signature>RU4wMDAwMDFFMTBBREMzOTQ5QkE1OUFCQkU1NkUwNTdGMjBGODgzRTM2OWI0NjljLTUxYjItNDNjZC05Njc3LTkzNGNhMTdmMjY1MQ==</Signature>\n" +
//                "  </IdentityInfo>\n" +
//                "  <ScrollingInfo>\n" +
//                "    <DisplayReq>30</DisplayReq>\n" +
//                "    <PageItems>10</PageItems>\n" +
//                "    <PageNo>1</PageNo>\n" +
//                "  </ScrollingInfo>\n" +
//                "  <!--查询条件-->\n" +
//                "  <SearchConditions>\n" +
//                "    <CountryId>0001</CountryId>\n" +
//                "    <ProvinceId>\n" +
//                "    </ProvinceId>\n" +
//                "    <CityId>\n" +
//                "    </CityId>\n" +
//                "    <HotelId>\n" +
//                "    </HotelId>\n" +
//                "    <RoomId>\n" +
//                "    </RoomId>\n" +
//                "    <RatePlanId>\n" +
//                "    </RatePlanId>\n" +
//                "    <StayDateRange>\n" +
//                "      <CheckIn>2021-03-24</CheckIn>\n" +
//                "      <CheckOut>2021-03-26</CheckOut>\n" +
//                "    </StayDateRange>\n" +
//                "    <GuestInfo>\n" +
//                "      <AdultCount>\n" +
//                "      </AdultCount>\n" +
//                "      <ChildCount>\n" +
//                "      </ChildCount>\n" +
//                "      <ChildAges>\n" +
//                "      </ChildAges>\n" +
//                "    </GuestInfo>\n" +
//                "    <Currency>\n" +
//                "    </Currency>\n" +
//                "    <Lang>\n" +
//                "    </Lang>\n" +
//                "    <RatePlanOnly>\n" +
//                "    </RatePlanOnly>\n" +
//                "  </SearchConditions>\n" +
//                "</CNRequest>";
//        String result = callAPIFunc("application/x-www-form-urlencoded", "http://202.104.101.32:8077/RESTServer.asmx/GetJsonData", params,"POST");
//        System.out.println(result);
    }

}
